package cn.iocoder.yudao.module.trade.service.giftcard;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.giftcard.vo.WxCardPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.giftcard.vo.WxCardSaveReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.giftcard.GiftWxCardDO;
import cn.iocoder.yudao.module.trade.dal.mysql.giftcard.GiftWxCardMapper;
import cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
public class GiftWxCardServiceImpl implements GiftWxCardService {

    @Resource
    private GiftWxCardMapper giftWxCardMapper;

    @Override
    public List<GiftWxCardDO> getGiftWxCardDOList() {
        return giftWxCardMapper.selectGiftWxCardOrderByCreateTime();
    }

    @Override
    public GiftWxCardDO selectByCardId(String cardId) {
        return giftWxCardMapper.selectByCardId(cardId);
    }


    @Override
    public Long createWxCard(WxCardSaveReqVO createReqVO) {
        // 插入
        GiftWxCardDO wxCard = cn.iocoder.yudao.framework.common.util.object.BeanUtils.toBean(createReqVO, GiftWxCardDO.class);

        List<JSONObject> blessingsArr = createReqVO.getBlessingsArr();
        String blessings = null;
        List<String> textArr = new ArrayList<>();
        if (null != blessingsArr && !blessingsArr.isEmpty()) {
            blessingsArr.forEach(x -> {
                textArr.add(x.getString("detailText"));
            });
        }
        blessings = StringUtils.join(textArr, ",");
        wxCard.setBlessings(blessings);
        wxCard.setCardId(UUID.randomUUID().toString().substring(0, 6));
        giftWxCardMapper.insert(wxCard);
        // 返回
        return wxCard.getId();
    }

    @Override
    public void updateWxCard(WxCardSaveReqVO updateReqVO) {
        // 校验存在
        validateWxCardExists(updateReqVO.getId());
        // 更新
        GiftWxCardDO updateObj = cn.iocoder.yudao.framework.common.util.object.BeanUtils.toBean(updateReqVO, GiftWxCardDO.class);

        List<JSONObject> blessingsArr = updateReqVO.getBlessingsArr();
        String blessings = null;
        List<String> textArr = new ArrayList<>();
        if (null != blessingsArr && !blessingsArr.isEmpty()) {
            blessingsArr.forEach(x -> {
                textArr.add(x.getString("detailText"));
            });
        }
        blessings = StringUtils.join(textArr, ",");
        updateObj.setBlessings(blessings);

        giftWxCardMapper.updateById(updateObj);
    }

    @Override
    public void deleteWxCard(Long id) {
        // 校验存在
        validateWxCardExists(id);
        // 删除
        giftWxCardMapper.deleteById(id);
    }

    private void validateWxCardExists(Long id) {
        if (giftWxCardMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.GiftWxCardDO);
        }
    }

    @Override
    public GiftWxCardDO getWxCard(Long id) {
        return giftWxCardMapper.selectById(id);
    }

    @Override
    public PageResult<GiftWxCardDO> getWxCardPage(WxCardPageReqVO pageReqVO) {
        return giftWxCardMapper.selectPage(pageReqVO);
    }

}
